/*
 * @Author: zlc
 * @Date: 2021-07-26 10:40:27
 * @LastEditTime: 2022-07-14 15:29:37
 * @LastEditors: zlc
 * @Description: uni-api二次封装
 * @FilePath: \teachernotice\common\util\uniPromise.js
 */

let ajaxTimes = 0
/**
 * @description: 上传文件-需要认证-不支持base64
 * @param {String} api 接口
 * @param {String} item 上传的文件
 * @param {String} type 上传到的文件路径
 * @param {String} title 提示语
 * @return {*}
 */
export const uploadFile = (api, item, type, title) => {
  ajaxTimes++
  //显示加载图片
  uni.showLoading({
    title: title ?? '上传中，请稍后',
    mask: true,
  })
  return new Promise((resolve, reject) => {
    let url = `${local.agreement}${local.getHostUrl()}:${local.port.http5}${api}`
    uni.uploadFile({
      url: url,
      //被上传文件的路径
      filePath: item,
      //上传文件的名称
      name: 'picture',
      header: {
        SYS_SIGN: 'APP',
        authorization: store.state.familyinfo.id,
      },
      formData: {
        type: type,
      },

      //顺带的文本新消息
      success: (result) => {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
      complete: (result) => {
        ajaxTimes--
        //所以等所有请求结束了才关闭提示框
        if (ajaxTimes === 0) {
          //不管失败还是成功，关闭加载图片
          uni.hideLoading()
        }
      },
    })
  })
}
/**
 * @description: 检测网络类型
 * @param {*}
 * @return {*}
 */
export const getNetworkType = () => {
  ajaxTimes++
  return new Promise((resolve, reject) => {
    uni.getNetworkType({
      success: function (result) {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
      complete: (result) => {
        ajaxTimes--
        //所以等所有请求结束了才关闭提示框
        if (ajaxTimes === 0) {
          //不管失败还是成功，关闭加载图片
          uni.hideLoading()
        }
      },
    })
  })
}

/**
 * @description: 获取系统信息
 * @param {*}
 * @return {*}
 */
export const getSystemInfo = () => {
  return new Promise((resolve, reject) => {
    uni.getSystemInfo({
      success: function (result) {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
    })
  })
}

/**
 * @description: 文件下载
 * @param {String} absolutePath 地址
 * @param {String} title 提示语
 * @return {*}
 */
export const downloadFile = (absolutePath, title) => {
  ajaxTimes++
  //显示加载图片
  uni.showLoading({
    title: title ?? '下载中',
    mask: true,
  })
  return new Promise((resolve, reject) => {
    uni.downloadFile({
      url: absolutePath,
      success: (result) => {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
      complete: (result) => {
        ajaxTimes--
        //所以等所有请求结束了才关闭提示框
        if (ajaxTimes === 0) {
          //不管失败还是成功，关闭加载图片
          uni.hideLoading()
        }
      },
    })
  })
}

/**
 * @description: 保存图片
 * @param {*} imagePath 图片地址
 * @param {*} quality 图片清晰度
 * @return {*}
 */
export function saveImageToPhotosAlbum(imagePath, quality) {
  ajaxTimes++
  //显示加载图片
  uni.showLoading({
    title: '加载中',
    mask: true,
  })
  return new Promise((resolve, reject) => {
    uni.saveImageToPhotosAlbum({
      filePath: imagePath,
      success: (result) => {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
      complete: (result) => {
        ajaxTimes--
        //所以等所有请求结束了才关闭提示框
        if (ajaxTimes === 0) {
          //不管失败还是成功，关闭加载图片
          uni.hideLoading()
        }
      },
    })
  })
}

/**
 * @description: 调起客户端小程序设置界面-小程序
 * @param {*}
 * @return {*} 返回用户设置的操作结果
 */
export const openSetting = () => {
  return new Promise((resolve, reject) => {
    uni.openSetting({
      success(result) {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
    })
  })
}
/**
 * @description: 获取用户的当前设置-小程序
 * @param {*}
 * @return {*}
 */
export const getSetting = () => {
  return new Promise((resolve, reject) => {
    uni.getSetting({
      success(result) {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
    })
  })
}

/**
 * @description: 设置系统剪贴板的内容
 * @param {String||Number} data 要复制的内容
 * @return {*}
 */
export const setClipboardData = (data) => {
  return new Promise((resolve, reject) => {
    uni.setClipboardData({
      data: data,
      success(result) {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
    })
  })
}
/**
 * @description: 获取Dom节点
 * @param {String} 节点 .class
 * @return {*}
 */
export const getSystemInfoCalendarSelectorQuery = (classDom) => {
  return new Promise((resolve, reject) => {
    const query =  uni.createSelectorQuery().in(this)
    query
      .select(classDom)
      .boundingClientRect((rect) => {
        console.log(rect);
        resolve(rect)
      })
      .exec()
  })
}

/**
 * @description: 普通信息提示框
 * @param {String} title 文本内容
 * @param {Object} options 其他参数
 * @return {*}
 * this.$showToast("xx");
 */
export const showToast = (title, options) => {
  let _options = Object.assign(
    {
      mask: true,
      icon: 'none',
      duration: 1500,
      position: 'center',
    },
    options
  )
  uni.showToast({
    title,
    ..._options,
  })
}
/**
 * @description: 微信支付
 * @param {*} title
 * @param {*} miniPayRequest
 * @return {*}
 */
export const requestPayment = (miniPayRequest) => {
  return new Promise((resolve, reject) => {
    wx.requestPayment({
      timeStamp: miniPayRequest.timeStamp,
      nonceStr: miniPayRequest.nonceStr,
      package: miniPayRequest.package,
      signType: miniPayRequest.signType,
      paySign: miniPayRequest.paySign,
      success(result) {
        resolve(result)
      },
      fail: (error) => {
        reject(error)
      },
    })
  })
}


export const addInterceptor=(options) =>{
  return new Promise((resolve, reject) =>{
    uni.addInterceptor(options,{
      success: (result) => {
        resolve(result)
      },
      fail(error) {
        reject(error)
      }, 
      complete(res) {
        
      }
    })
  })

}
/**
 * @description: 设置是否打开调试开关。此开关对正式版也能生效
 * @param {option} Boolean
 * @param {*} 
 * @return {*}
 */
export const setEnableDebug=(option=true)=>{

  return new Promise((resolve,reject)=>{
    uni.setEnableDebug({
      enableDebug: option,
      success: (result) => {
        resolve(result)
      },
      fail(error) {
        reject(error)
      }, 
     
    })
  })
  
}
/**
 * @description: 打开相机
 * @param {*} option
 * @return {*}
 */
export const chooseImage=(option)=>{
  let chooseImageOption={
    count: 1,
    sizeType: ['original'],
    sourceType: ['album', 'camera'],
  }
  return new Promise((resolve,reject)=>{
    uni.chooseImage({
      ...Object.assign(chooseImageOption,option),
      success: (result) => {
        resolve(result)
      },
      fail(error) {
        reject(error)
      }, 
     
    })
  })
  
}


/**
 * @description: 默认加载提示
 * @param {*} options
 * @return {*}
 */
export const showLoading=(options) =>{
  let _options = Object.assign(
    {
      mask: true,
		  title: '加载中',
    },
    options
  )
  uni.showLoading({
		..._options
	})
}
/**
 * @description: 加载本地字体
 * @param {String} source 字体文件
 * @return {String} family 字体名称
 */
export const loadFontFace=(source,family='font-type')=>{
  //console.log(`url(${staticEnvironmentUrl()}emailapplet/static/font/order/SimHei.ttf)`);
  return new Promise((resolve,reject)=>{
    uni.loadFontFace({
      global: true,
      family,
      source,
      success(res){
        resolve(res)
      },
      fail: function (res) {
        console.log("loadFontFaceFail",res)
      },
      complete: function (res) {
        console.log("loadFontFaceComplete",res)
      }
    })
  })
  
}


/**
 * @description: 获取微信用户头像昵称
 * @return {*} 
 */
 export const getUserProfile=()=>{
  return new Promise((resolve,reject)=>{
    uni.getUserProfile({
			desc: "用于完善用户信息",
			success: (res) => {
        resolve(res)
			},
			fail: (err) => {
				reject(err)
			}	
		})
  })
  
}


/**
 * @description: 检查登录有效性
 * @return {*} 
 */
export const checkSession=()=>{
  return new Promise((resolve, reject) => {
    wx.checkSession({
      success() {
        return resolve(true)
      },
      fail() {
        return resolve(false)
      }
    })
  })
}